var app = sm("do_App");
var page = sm("do_Page");
var hashdata = mm("do_HashData");
var common = require("common");
var urlUser = require("url/user");
var userUtil = require("helper/user");
var http = require("helper/http");
var nf = sm("do_Notification");
var imHelper = require("helper/im");
var push = require("helper/push");
var deviceUtil = require("helper/device");

var root = ui("$");
page.on("back", function() {
    app.closePage();
});
var dataFromCode = page.getData();

var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "请留意手机短信"
})
header.refreshData();

var loginBtn = ui("loginBtn");
var loginForm = ui("loginForm");
var addVerifyCode = loginForm.add("verifyCode", "source://view/reuse/textField.ui",0,0);
var verifyCode = ui("verifyCode");
verifyCode.bindData(hashdata);
hashdata.addData({
    "textFieldHint" : "输入验证码",
    "textFieldInputType" : "PHONENUMBER"
});
verifyCode.refreshData();

var addBtnLogin = loginBtn.add("btnLogin", "source://view/reuse/button.ui",0,0);
var btnLogin = ui("btnLogin");
btnLogin.bindData(hashdata);
hashdata.addData({
    "btnText" : "确认提交"
});
btnLogin.refreshData();
btnLogin.on("touch", function(data,e){
	page.hideKeyboard();//隐藏键盘
	if(!dataFromCode || !dataFromCode.phone || dataFromCode.phone.trim()===""){
		nf.toast("系统异常");
		return;
	}
	var verifyCodeValue = ui(addVerifyCode+".textField").text;
	if(verifyCodeValue.trim()===""){
		nf.toast("请输入验证码");
		return;
	}
	http.post(urlUser.url.login.doLoginWithPhoneCode,{"phone":dataFromCode.phone.trim(),"verifycode":verifyCodeValue,"registrationId":push.getRegistrationID(),"deviceId":deviceUtil.getInfo().deviceId}, function(data) {
		userUtil.loginToken(data.loginUser.token);
		userUtil.userId(data.loginUser.id);
		userUtil.userLogo(data.logo);
		if(data.hasIdentityAuth && data.hasIdentityAuth!=null && data.hasIdentityAuth!=="undefined"){
			userUtil.hasIdentityAuth(data.hasIdentityAuth);
		}
		if(data.hasCommunityAuth && data.hasCommunityAuth!=null && data.hasCommunityAuth!=="undefined"){
			userUtil.hasCommunityAuth(data.hasCommunityAuth);
		}
		push.handleAudience();
		imHelper.loginIM(function(){
			nf.toast("登录成功");
			if(data.register&&data.register==="1"){
				app.fire("fireUserUpdate",{"logo":data.logo,"nick":data.nick});
				common.open("source://view/user/initUser.ui",{"nick":data.nick,"strPwd":data.strPwd});
			}else{
				app.fire("fireUserUpdate",{"logo":data.logo,"nick":data.nick});
				app.closePageToID("","","qquan_page_index");//返回index
			}
		});
	});
});

var btnLoginPwd = ui("btnLoginPwd");
btnLoginPwd.on("touch", function(data,e){
	common.open("source://view/user/login.ui");
});
page.on("result", function(data) {
	if(data&&data!=null&&data!=="undefined"&&data.isLogin==="1"){
		app.closePage(data);
	}
});
//点击背景隐藏输入键盘
root.on("touch",function(){
	page.hideKeyboard();
})